package net.lwf.jaxb.service.tabmenu;

import java.util.List;

import net.lwf.jaxb.service.JaxbService;
import net.lwf.jaxb.stubs.tabmenu.Grade;
import net.lwf.jaxb.stubs.tabmenu.SubjectInfo;
import net.lwf.jaxb.stubs.tabmenu.TabMenuInfo;
import net.lwf.jaxb.stubs.tabmenu.TopicInfo;


public class TabMenuService extends JaxbService {
	
	private String grade;
	private static final String CONTEXT_PATH = "net.lwf.jaxb.stubs.tabmenu";
	private String xmlFileName = "tabmenu/TabMenuInfo";
	private String fileExtension = ".xml";
	
	public List<SubjectInfo> getMenu(String in_grade) {
		this.grade = in_grade;
		TabMenuInfo list = (TabMenuInfo) loadedXmlData();
		Grade grade = list.getGrade();
		if(grade.getName().equalsIgnoreCase(in_grade)) {
			return grade.getSubjectInfo();
		}

		return null;
	}
	
	public SubjectInfo getMenu(String in_grade, String in_subject) {
		List<SubjectInfo> subjects = getMenu(in_grade);
		for(SubjectInfo subject: subjects) {
			if(subject.getName().equalsIgnoreCase(in_subject)) {
				return subject;
			}
		}
		return null;
	}
	
	public TopicInfo getMenu(String in_grade, String in_subject, String in_topic) {
		List<TopicInfo> topics = getMenu(in_grade, in_subject).getTopicInfo();
		for(TopicInfo topic: topics) {
			if(topic.getName().equalsIgnoreCase(in_topic)) {
				return topic;
			}
		}
 		return null;
	}
	

	@Override
	public String getContextPath() {
		return CONTEXT_PATH;
	}

	@Override
//	public String getXmlFileName() {
//		return xmlFileName;
//	}
	public String getXmlFileName() {
		return xmlFileName + this.grade + fileExtension;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TabMenuService menu = new TabMenuService();
		System.out.println("===========> By Grade Input");
		List<SubjectInfo> subjects = menu.getMenu("1");
		for(SubjectInfo subject: subjects) {
			subject.print();
		}
		System.out.println("\n===========> By Grade Input, Subject Input");
		menu = new TabMenuService();
		SubjectInfo subject = menu.getMenu("2", "english");
		subject.print();
		
		System.out.println("\n===========> By Grade Input, Subject Input, Topic Input");
		menu = new TabMenuService();
		TopicInfo topic = menu.getMenu("1", "Maths", "algebra");
		topic.print();
	}

}

/*
public class TabMenuService {
	
	private static final String CONTEXT_PATH = "net.lwf.jaxb.stubs.tabmenu";
	
	private XmlFileInfo xmlFileInfo;
	private String xmlFileName = "TabMenuInfo.xml";

	private TabMenuInfo list = null;
	
	public TabMenuService() {
		this.loadXmlData();
	}
	
	public List<SubjectInfo> getMenu(String in_grade) {
		List<Grade> gradeinfo = list.getGrade();
		for(Grade grade: gradeinfo) {
			if(grade.getName().equalsIgnoreCase(in_grade)) {
				return grade.getSubjectInfo();
			}
		}
		return null;
	}
	
	public SubjectInfo getMenu(String in_grade, String in_subject) {
		List<SubjectInfo> subjects = getMenu(in_grade);
		for(SubjectInfo subject: subjects) {
			if(subject.getName().equalsIgnoreCase(in_subject)) {
				return subject;
			}
		}
		return null;
	}
	
	public TopicInfo getMenu(String in_grade, String in_subject, String in_topic) {
		List<TopicInfo> topics = getMenu(in_grade, in_subject).getTopicInfo();
		for(TopicInfo topic: topics) {
			if(topic.getName().equalsIgnoreCase(in_topic)) {
				return topic;
			}
		}
 		return null;
	}
	
	private void loadXmlData() {
		loadXml();
		try {
			JAXBContext jc = JAXBContext.newInstance(CONTEXT_PATH);
			Unmarshaller u = jc.createUnmarshaller();
			list = (TabMenuInfo) u.unmarshal(xmlFileInfo.getInputStream());
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}
	
	private synchronized void loadXml() {
		if (xmlFileInfo == null) {
			xmlFileInfo = new XmlFileInfo(xmlFileName);
		} else {
			if (xmlFileInfo.isChanged() == false) {
				return;
			}
		}
	}

	*//**
	 * @param args
	 *//*
	public static void main(String[] args) {
		TabMenuService menu = new TabMenuService();
		System.out.println("===========> By Grade Input");
		List<SubjectInfo> subjects = menu.getMenu("1");
		for(SubjectInfo subject: subjects) {
			subject.print();
		}
		System.out.println("\n===========> By Grade Input, Subject Input");
		SubjectInfo subject = menu.getMenu("2", "g2-s2");
		subject.print();
		
		System.out.println("\n===========> By Grade Input, Subject Input, Topic Input");
		TopicInfo topic = menu.getMenu("1", "g1-s1", "s1-t1");
		topic.print();
	}
}*/
